<?php

// $users = DB::select('select * from lv_test where active = ?', [1]);
// $tests = \Illuminate\Support\Facades\DB::select('select * from lv_test');
$tests = \DB::select('select * from lv_test');
// stdClass 对象的数组；




通过中间件设置查看执行的 SQL 语句
\app\Http\Middleware\DiyQueryStatement.php
    namespace App\Http\Middleware;

    use Closure;
    use Illuminate\Support\Facades\Auth;

    class DiyQueryStatement
    {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle($request, Closure $next)
        {

            // 试图在中间件内添加查看SQL 语句的功能，失败了；放在公共父类的前置方法中就行了。
            // \Illuminate\Support\Facades\DB::listen(function($sql, $bindings, $time) {
            //     echo '<br/><br/><hr/>SQL语句执行：<br/>'. $sql
            //         . '<br/>参数<br/>' . json_encode($bindings)
            //         . '<br/>耗时<br/>' . $time
            //         . 'ms<hr/><br/><br/>';
            // });
            
            \DB::listen(function ($query){
                $sql = $query->sql;
                $bindings = $query->bindings;
                $time = $query->time;
                echo '<br/><br/><hr/><h1>D:\project\study\study-php-laravel\laravel5331\app\Http\Middleware\DiyQueryStatement.php</h1><hr/><br/>';
                echo '<br/><br/><hr/><h1>SQL语句执行</h1><br/>'. $sql
                    . '<br/>参数<br/>' . json_encode($bindings)
                    . '<br/>耗时<br/>' . $time
                    . 'ms<hr/><br/><br/>';

                // if($time>10){  //when time > 10 print
                //    \Log::debug(var_export(compact(['sql','bindings','time']),true));
                // }
            });

            return $next($request);
        }
    }
然后在 \app\Http\Kernel.php 中添加
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,

        // 添加这一行即可，对所有路由都有作用；
        \App\Http\Middleware\DiyQueryStatement::class,
    ];